package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.util.ConfigUtil;

import com.util.BODY;
import com.util.Constant;
import com.util.HttpRequest;
import com.util.JSONUtil;
import com.util.MD5;
import com.util.OpenKey;
import com.util.ReadConfig;
import com.util.SessionInfo;


@Controller
@RequestMapping("/ywzxcontrller")
public class ywzxcontrller {

	private static final Logger logger = Logger.getLogger(ywzxcontrller.class);
	Map map  = new HashMap();
	List list = new ArrayList();
	  
	@RequestMapping("/test")
	@ResponseBody
	public String entryinterface(HttpServletRequest request, HttpServletResponse response,HttpSession session , String limit ,String id,String start,String end,String ywlsh) throws IOException{
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		String interfaceUrl = ReadConfig.readValue();
		Map remap = new HashMap();
		BODY body = new BODY();
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
		String Key="";
		if(sessionIn!=null){
			if(!(sessionIn.getKEY().equals("")|| sessionIn.getKEY()==null)){
				 Key=sessionIn.getKEY();
			}
		}
		String a="";
		String Timestamp=sdf.format(date);
		String jm=Constant.mima+Timestamp+Constant.miyao;
		// 接口编码
		System.out.println(jm);
		MD5 md5=new MD5();
		a=md5.getMD5(jm).toString();
			body.setPassWd(a);
				remap.put("AUTHORITY", "YBGJJ100");
				// 接口参数
				remap.put("LOGUserName",Constant.username);
				remap.put("BODY", body);
				body.setTimestamp(Timestamp);
				String json3 = JSONUtil.ToJsongString2(remap);
				// 请求
				String result2 = HttpRequest.doPost(interfaceUrl, json3);
				logger.info("接口返回内容:" + result2);
				Map retMap2 = JSONUtil.toMap(result2);
				
					if(retMap2.get("RETURNCODE").equals("100")){
						Map mx2 = JSONUtil.toMap(JSONUtil.ToJsongString2(retMap2.get("body")));
						List jx = (List) mx2.get("RETDATA");
						Map m = (Map) jx.get(0);
						Key += m.get("OpenKey").toString();		
					}else{
						map.put("code", 1);
						map.put("CW", retMap2.get("RETURNDESC").toString());
						list.add(retMap2);
						response.setHeader("Content-type", "text/html;charset=UTF-8");
						response.setCharacterEncoding("UTF-8");
						String jsonString = JSONUtil.ToJsongString2(map);
						PrintWriter pw = response.getWriter();
						pw.write(jsonString);
						return null;
					}
		Map reqMap = new HashMap();
		BODY b=new BODY();
		OpenKey opk=new OpenKey();
		reqMap.put("AUTHORITY", "YBGJJ113");
		//接口参数
		reqMap.put("OpenKey",sessionIn.getKEY());
		reqMap.put("BODY",b);
		OpenKey opk1=new OpenKey();
		String result=opk1.getOpenKey(reqMap, session);
		Map retMap = JSONUtil.toMap(result);
		logger.info("接口返回内容:"+retMap);
		if(retMap.get("RETURNCODE").equals("100")){
			Map resMap=JSONUtil.toMap(JSONUtil.ToJsongString2(retMap.get("body")));
			List retlist = (List)resMap.get("RETDATA");
			for (int i = 0; i < retlist.size(); i++) {
				Map map = (Map) retlist.get(i);
				map.put("XINGMING", sessionIn.getXINGMING().toString());	
			}
			
			logger.info("接口返回内容:"+result);
			if(result != null){
				if(retMap.get("RETURNCODE").equals("100")){
				/*	List resList = JSONUtil.toList(resMap.get("body").toString());*/
					//map.put("count", resList.size());
					map.put("code", 0);
					map.put("msg", "1");
					map.put("data",retlist);
					list.add(resMap);
					response.setHeader("Content-type", "text/html;charset=UTF-8");
					response.setCharacterEncoding("UTF-8");
					String jsonString = JSONUtil.ToJsongString2(map);
					PrintWriter pw = response.getWriter();
					pw.write(jsonString);
				}
			}else{
				map.put("code", 1);
				map.put("msg", "1");
				map.put("CW", retMap.get("RETURNDESC").toString());
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
				return null;
			}
		}else{
			map.put("code", 1);
			map.put("msg", "1");
			map.put("CW", retMap.get("RETURNDESC").toString());
			response.setHeader("Content-type", "text/html;charset=UTF-8");
			response.setCharacterEncoding("UTF-8");
			String jsonString = JSONUtil.ToJsongString2(map);
			PrintWriter pw = response.getWriter();
			pw.write(jsonString);
			return null;
		}
		return null;
	}
}
